#ifndef __GT_IIC_H
#define __GT_IIC_H

/*
    GT1151Q触摸芯片
    date: 2024-11-18
    author: yss
*/


#include "stm32f4xx.h"


/*****触摸屏关键参数*****/

typedef struct 
{
    uint16_t x[5]; // 最多存放5个手指的x坐标
    uint16_t y[5]; // 最多存放5个手指的y坐标
    uint8_t fg_num; //手指数量
    uint8_t tp_sta; //触摸状态
}tp_device;

extern tp_device tp_dev;

/***********************/

#define GT_WRITE_ADDR  0x28
#define GT_READ_ADDR   0x29

/* 相关寄存器地址 */
// 连续读取四个寄存器
#define ContrlReg   0x8040
#define ProductID   0X8140

#define StatusReg   0x814E
#define Point1Reg   0x8150
#define Point2Reg   0x8158
#define Point3Reg   0x8160
#define Point4Reg   0x8168 
#define Point5Reg   0x8170
/* 相关寄存器地址 */

// 应答/非应答
#define IIC_ACK     0
#define IIC_NACK    1

// RST --- T_CS --- PD13
// INT --- T_PEN --- PE4
// SDA --- T_MOSI --- PE2
// SCL --- T_SCL --- PE0
#define IIC_PORT    GPIOE
#define IIC_CLK     RCC_AHB1Periph_GPIOE
#define IIC_SDA     GPIO_Pin_2
#define IIC_SCL     GPIO_Pin_0

#define IIC_SDA_SET(x)  (x) ? (IIC_PORT->ODR |= IIC_SDA):\
                                (IIC_PORT->ODR &= ~IIC_SDA)
#define READ_SDA    (IIC_PORT->IDR & IIC_SDA)

#define IIC_SCL_SET(x)  (x) ? (IIC_PORT->ODR |= IIC_SCL):\
                                (IIC_PORT->ODR &= ~IIC_SCL)
#define READ_SCL    (IIC_PORT->IDR & IIC_SCL)



#define RST_PORT    GPIOD
#define RST_CLK     RCC_AHB1Periph_GPIOD
#define RST_PIN     GPIO_Pin_13
#define RST_SET(x)  (x) ? (RST_PORT->ODR |= RST_PIN):\
                            (RST_PORT->ODR &= ~RST_PIN)

#define INT_PORT    GPIOE
#define INT_CLK     RCC_AHB1Periph_GPIOE
#define INT_PIN     GPIO_Pin_4
#define READ_INT    (INT_PORT->IDR & INT_PIN)


uint8_t Touch_Scan(void);

uint8_t GT1151Q_Init(void);
uint8_t GT_Write_Reg(uint8_t *buf,uint8_t size,uint16_t addr);
void GT_Read_Reg(uint8_t *buf, uint8_t size, uint16_t addr);


#endif // __GT_IIC_H